﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using HtmlAgilityPack;

namespace autoBidProject
{
    public partial class Main : Form
    {
        Form optionForm;
        Form mapForm;

        private MatchManager matchManager;

        public Main()
        {
            InitializeComponent();

            optionForm = new Options();
            mapForm = new Map();
            matchManager = new MatchManager();

            if (!Setting.LoadSetting())
            {
                MessageBox.Show("Không thể load cấu hình.\nBạn phải thiết lập cấu hình trước khi sử dụng.","Không thể load cấu hình",MessageBoxButtons.OKCancel,MessageBoxIcon.Error,MessageBoxDefaultButton.Button1);
            }
        }

        private void Main_Load(object sender, EventArgs e)
        {
            //scMain.IsSplitterFixed = true;
            scMain.Panel1MinSize = 200;
            scMain.SplitterDistance = 200;

            wbIBET.Navigate(Setting.Url_Ibet);
            wbSBO.Navigate(Setting.Url_Sbo);

            timerMainUpdate.Interval = 2000;//Setting.TimeRefreshSBO;
            timerMainUpdate.Start();
        }

        private void timerMainUpdate_Tick(object sender, EventArgs e)
        {
            RefreshSite();
            MainUpdate();
        }

        private void RefreshSite()
        {
            IBET.Update(wbIBET.Document);
            //if (wbIBET.ReadyState == WebBrowserReadyState.Complete)
                matchManager.IbetLeagues = IBET.LoadFromIBET(wbIBET.Document, Setting.bEventType);

            SBO.Update(wbSBO.Document);
            //if (wbSBO.ReadyState == WebBrowserReadyState.Complete)
                 matchManager.SboLeagues =  SBO.LoadFromSBO(wbSBO.Document, Setting.bEventType); //Setting.bEventType
        }

        int i = 0;
        private void MainUpdate()
        {
            System.Windows.Forms.HtmlDocument sboHtmlDoc = wbSBO.Document;

            List<Match> ibetMappedMatchs = matchManager.GetMappedMatchs(GlobalVar.MatchType.IBET);
            foreach (Match ibetMatch in ibetMappedMatchs)
            {
                Match sboMatch = matchManager.GetRespectiveMatch(GlobalVar.MatchType.IBET, ibetMatch.Name());
                foreach (Event iEvent in ibetMatch.Events)
                {
                    Event sEvent = sboMatch.GetEvent(true, iEvent.ft_hdp);
                    if (sEvent != null)
                        CheckEventForBid(iEvent, sEvent);
                }
            }        
        }

        private void CheckEventForBid(Event ibetEvent, Event sboEvent)
        {
            if (ibetEvent.ft_hdp == sboEvent.ft_hdp)
            {
                if (ibetEvent.ft_home > sboEvent.ft_home)
                {
                    if (KiemTraGia0123(ibetEvent.ft_home, sboEvent.ft_away))
                    {
                        ibetEvent.elementSelected = GlobalVar.ElementType.FT_HOME;
                        sboEvent.elementSelected = GlobalVar.ElementType.FT_AWAY;

                        dgBidFinded.Rows.Add(new string[] { "IBET", "", ibetEvent.ft_hdp, "", ibetEvent.Name() });
                    }
                    if (KiemTraGia0123(ibetEvent.ft_away, sboEvent.ft_home))
                    {
                        ibetEvent.elementSelected = GlobalVar.ElementType.FT_AWAY;
                        sboEvent.elementSelected = GlobalVar.ElementType.FT_HOME;

                        dgBidFinded.Rows.Add(new string[] { "IBET", "", ibetEvent.ft_hdp, "", ibetEvent.Name() });
                    }
                    //MessageBox.Show(ibetEvent.Name() +"\n"+sboEvent.Name());
                }
                else
                    if (KiemTraGia0123(sboEvent.ft_home, ibetEvent.ft_away) || KiemTraGia0123(sboEvent.ft_away, ibetEvent.ft_home))
                    {
                        dgBidFinded.Rows.Add(new string[] { "SBO", "", sboEvent.ft_hdp, "", sboEvent.Name() });
                    }
                //   MessageBox.Show(ibetEvent.Name() + "\n" + sboEvent.Name());
            }
            else if (ibetEvent.fh_hdp == sboEvent.fh_hdp)
            {
                if (ibetEvent.ft_home > sboEvent.ft_home)
                {
                    if (KiemTraGia0123(ibetEvent.fh_home, sboEvent.fh_away) || KiemTraGia0123(ibetEvent.fh_away, sboEvent.fh_home))
                    {
                        dgBidFinded.Rows.Add(new string[] { "IBET", "", ibetEvent.fh_hdp, "", ibetEvent.Name() });
                    }
                    //MessageBox.Show(ibetEvent.Name() +"\n"+sboEvent.Name());
                }
                else
                    if (KiemTraGia0123(sboEvent.fh_home, ibetEvent.fh_away) || KiemTraGia0123(sboEvent.fh_away, ibetEvent.fh_home))
                    {
                        dgBidFinded.Rows.Add(new string[] { "SBO", "", sboEvent.fh_hdp, "", sboEvent.Name() });
                    }
                //   MessageBox.Show(ibetEvent.Name() + "\n" + sboEvent.Name());
            }

        }

        private bool KiemTraGia0123(float ibetGia, float sboGia)
        {
            if (ibetGia != -1 && sboGia != -1)
            {
                ibetGia = Math.Abs(ibetGia) * 100;
                sboGia = Math.Abs(sboGia) * 100;
                float hieu = ibetGia - sboGia;
                hieu = Math.Abs(hieu);
                int gia = (int)Math.Floor(hieu);

                switch (gia)
                {
                    case 0: return Setting.bGia0;
                    case 1: return Setting.bGia1;
                    case 2: return Setting.bGia2;
                    case 3: return Setting.bGia3;
                }
            }
            return false;
        }

        /*
        if (e.ft_home > 0.7f)
        {
            HtmlElement table = SBO.GetTableEvents();
            if (table != null)
            {
                if (e.ft_home_id != "")
                {
                    HtmlElement item = table.Document.GetElementById(e.ft_home_id);
                    if (item != null)
                    {
                        i++;
                        item.InvokeMember("click");
                    }
                }
            }
        } */

        private void optionToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Options frm = new Options();
            frm.ShowDialog();
        }

        private void btRefreshIBET_Click(object sender, EventArgs e)
        {
            wbIBET.Refresh();
        }

        private void mapToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Map frm = new Map();
            frm.Tag = matchManager;
            frm.ShowDialog();
            matchManager = (MatchManager) frm.Tag;
        }

        private void btRefreshSBO_Click(object sender, EventArgs e)
        {
            wbSBO.Refresh();
        }

        private bool bAutoPlay = false;
        private void btAutoBid_Click(object sender, EventArgs e)
        {
            bAutoPlay = true;
        }
    }
}



